//
//  FriendSetViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/4/8.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "FriendSetViewController.h"
#import "SelectGroupViewController.h"

#define kCurrentUserToken [[NSUserDefaults standardUserDefaults]objectForKey:@"token"]

@interface FriendSetViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *remarkTF; // 备注
@property (nonatomic, strong) UILabel *groupLabel; // 分组

@property (nonatomic, assign) __block BOOL canClickFinish; // 是否可以点击完成

@property (nonatomic, assign) __block BOOL shouldRefresh; // 是否需要刷新界面

@end

@implementation FriendSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // navi的修改
    [self changeNavigation];
    // 布局
    [self addMySubview];
    self.canClickFinish = YES;
    self.shouldRefresh = NO;
    NSLog(@"好友设置分组的名字 = %@", self.fenZuName);
    NSLog(@"好友设置分组名字对应的字典ID = %@", self.fenZuNameDuiYingDic);
}

// navi的修改
- (void)changeNavigation
{
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationItem.title = @"好友设置";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    UIBarButtonItem *rightBarItem =  [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(finish:)];
    rightBarItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    if (self.shouldRefresh == YES) {
        self.zgwFriendSetVCBlock(@"1");
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 完成
- (void)finish:(UIBarButtonItem *)barItem
{
    NSLog(@"完成");
    NSLog(@"分组:%@", self.groupLabel.text);
    NSLog(@"备注:%@", self.remarkTF.text);
    // 获取分组对应的ID
    NSString *groupID = [self.fenZuNameDuiYingDic valueForKey:self.groupLabel.text];
    NSLog(@"groupID = %@", groupID);
    if (self.canClickFinish == YES) {
        self.canClickFinish = NO;
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] startAnimation];
        // 同意加好友的url
        NSString *agreeFriendUrl = [NSString stringWithFormat:@"%@?token=%@", kZGWAgreeFriendApply, kCurrentUserToken];
        NSDictionary *paramaterDic = @{@"friend_ship_request_id":self.applyID, @"friend_group_id":groupID};
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager POST:agreeFriendUrl parameters:paramaterDic success:^(NSURLSessionDataTask *task, id responseObject) {
            NSDictionary *statusDic = [responseObject valueForKey:@"status"];
            NSString *msgString = [statusDic valueForKey:@"msg"];
            if ([msgString isEqualToString:@"created success"]) {
                // 添加成功
                self.shouldRefresh = YES;
            //    [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertNoTitieWithContent:@"添加成功" toView:self];
                [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertWithAnimationByTitle:@"添加成功" toView:self];
            }
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
            self.canClickFinish = YES;
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
            self.canClickFinish = YES;
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"提示" Content:@"添加失败，请重试" ToView:self];
        }];
    }
    
}

#pragma mark - 布局
- (void)addMySubview
{
    // 顶部灰色条
    UILabel *topGrayLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 15 * kAdjustParamater)];
    topGrayLine.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.03];
    [self.view addSubview:topGrayLine];
    
//    // 备注后面的白色背景
//    UIView *whiteViewOnBeiZhu = [[UIView alloc] initWithFrame:CGRectMake(0, topGrayLine.bottom, kScreenWidth, 46 * kAdjustParamater)];
//    whiteViewOnBeiZhu.backgroundColor = [UIColor whiteColor];
//    whiteViewOnBeiZhu.userInteractionEnabled = YES;
//    [self.view addSubview:whiteViewOnBeiZhu];
//    
//    // 备注
//    UILabel *beiZhuLabel = [[UILabel alloc] initWithFrame:CGRectMake(22 * kAdjustParamater, 15 * kAdjustParamater, 30 * kAdjustParamater, 15 * kAdjustParamater)];
// //   beiZhuLabel.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
//    beiZhuLabel.text = @"备注";
//    beiZhuLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
//    beiZhuLabel.font = [UIFont systemFontOfSize:k15Big];
//    [whiteViewOnBeiZhu addSubview:beiZhuLabel];
//    
//    // 备注TF
//    self.remarkTF = [[UITextField alloc] initWithFrame:CGRectMake(beiZhuLabel.right + 35 * kAdjustParamater, beiZhuLabel.top, 250 * kAdjustParamater, 15 * kAdjustParamater)];
// //   self.remarkTF.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
//    self.remarkTF.textColor = [UIColor blackColor];
//    self.remarkTF.text = self.markName;
// //   self.remarkTF.clearsOnBeginEditing = YES;
//    self.remarkTF.tintColor =  kCyanColor;
//    self.remarkTF.textAlignment = NSTextAlignmentLeft;
//    self.remarkTF.font = [UIFont systemFontOfSize:k15Big];
//    [self.remarkTF addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventAllEditingEvents];
//    [whiteViewOnBeiZhu addSubview:_remarkTF];
//    
//    // 备注下方灰色条
//    UILabel *grayLineOnBeiZhuBottom = [[UILabel alloc] initWithFrame:CGRectMake(0, 45 * kAdjustParamater, kScreenWidth, 1 * kAdjustParamater)];
//    grayLineOnBeiZhuBottom.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
//    [whiteViewOnBeiZhu addSubview:grayLineOnBeiZhuBottom];
//    
//    // 备注下方宽的灰色条
//    UILabel *grayWidthLineOnBeiZhuBottom = [[UILabel alloc] initWithFrame:CGRectMake(0, whiteViewOnBeiZhu.bottom, kScreenWidth, 13 * kAdjustParamater)];
//    grayWidthLineOnBeiZhuBottom.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.03];
//    [self.view addSubview:grayWidthLineOnBeiZhuBottom];
//    
//    // 宽的灰色条下方的灰色条
//    UILabel *grayLineBottom = [[UILabel alloc] initWithFrame:CGRectMake(0, grayWidthLineOnBeiZhuBottom.bottom, kScreenWidth, 1 * kAdjustParamater)];
//    grayLineBottom.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
//    [self.view addSubview:grayLineBottom];
    
    // 分组的白色背景
    UIView *whiteViewOnFenZu = [[UIView alloc] initWithFrame:CGRectMake(0, topGrayLine.bottom, kScreenWidth, 46 * kAdjustParamater)];
    whiteViewOnFenZu.backgroundColor = [UIColor whiteColor];
    whiteViewOnFenZu.userInteractionEnabled = YES;
    [self.view addSubview:whiteViewOnFenZu];
    
    // 为分组的白色背景添加手势
    UITapGestureRecognizer *tapGesOnFenZu = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goToFenZu:)];
    [whiteViewOnFenZu addGestureRecognizer:tapGesOnFenZu];
    
    // 分组
    UILabel *fenZuLabel = [[UILabel alloc] initWithFrame:CGRectMake(22 * kAdjustParamater, 15 * kAdjustParamater, 30 * kAdjustParamater, 15 * kAdjustParamater)];
 //   fenZuLabel.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
    fenZuLabel.text = @"分组";
    fenZuLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    fenZuLabel.font = [UIFont systemFontOfSize:k15Big];
    [whiteViewOnFenZu addSubview:fenZuLabel];
    
    // 分组label
    self.groupLabel = [[UILabel alloc] initWithFrame:CGRectMake(fenZuLabel.right + 35 * kAdjustParamater, fenZuLabel.top, 150 * kAdjustParamater, 15 * kAdjustParamater)];
 //   self.groupLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    self.groupLabel.textColor = [UIColor blackColor];
    // 默认为我的好友
    self.groupLabel.text = @"我的好友";
    self.groupLabel.font = [UIFont systemFontOfSize:k15Big];
    [whiteViewOnFenZu addSubview:_groupLabel];
    
    // 右边的灰色箭头
    UIImageView *grayJianTou = [[UIImageView alloc] initWithFrame:CGRectMake((375 - 30) * kAdjustParamater, 13 * kAdjustParamater, 20 * kAdjustParamater, 20 * kAdjustParamater)];
    grayJianTou.image = [UIImage imageNamed:@"grayJianTou"];
    [whiteViewOnFenZu addSubview:grayJianTou];
    
    // 分组下方的灰色箭头
    UILabel *grayLineOnFenZuBottom = [[UILabel alloc] initWithFrame:CGRectMake(0, 45 * kAdjustParamater, kScreenWidth, 1 * kAdjustParamater)];
    grayLineOnFenZuBottom.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [whiteViewOnFenZu addSubview:grayLineOnFenZuBottom];
    
    
    
    
    
    
    
}

#pragma mark - 输入框内容变化
- (void)textChange:(UITextField *)textField
{
    NSLog(@"textField = %@", textField.text);
}

#pragma mark - 点击分组
- (void)goToFenZu:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"分组");
    SelectGroupViewController *selectGroupVC = [[SelectGroupViewController alloc] init];
    __block FriendSetViewController *mySelf = self;
    selectGroupVC.selectGroupBlock = ^void(NSString *str) {
        if (str == nil) {
            mySelf.groupLabel.text = @"我的好友";
        } else {
            mySelf.groupLabel.text = str;
        }
    };
    selectGroupVC.fenZuMingZiArray = [NSMutableArray arrayWithArray:self.fenZuName];
    selectGroupVC.selectedGroupName = self.groupLabel.text;
    [self.navigationController pushViewController:selectGroupVC animated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
